import { Task } from "../entity/Task";
import Store from 'electron-store';

// 定义存储结构（使用 plain object）
interface StorageSchema {
  tasks: {
    id: number;
    name: string;
    cronExpression: string;
    filePaths: string[];
    isActive: boolean;
  }[];
}


// 创建 store 实例
const tempStore = new Store<StorageSchema>({
  defaults: {
    tasks: []
  }
});

export class TasksStore {
  /** 存储任务 */
  static saveTasks(tasks: Task[]): void {
    // tempStore.get("tasks") 返回的是一个副本（浅拷贝）数组。
    const plainTasks = tasks.map(task => ({
      id: task.id,
      name: task.name,
      cronExpression: task.cronExpression,
      filePaths: task.filePaths,
      isActive: task.isActive
    }));
    tempStore.set("tasks", plainTasks);
  }

  /** 获取所有任务 */
  static getTasks(): Task[] {
    const storedTasks = tempStore.get("tasks") || [];
    return storedTasks.map(
      (taskData) =>
        new Task(
          taskData.id,
          taskData.name,
          taskData.cronExpression,
          taskData.filePaths,
          taskData.isActive
        )
    );
  }

  /** 清除所有任务 */
  static clearTasks(): void {
    tempStore.delete("tasks");
  }
}
